package com.athuahua.spzx.service.impl;

import cn.hutool.core.date.DateTime;
import com.athuahua.spzx.mapper.SysUserMapper;
import com.athuahua.spzx.dto.system.AssginRoleDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.SysUserRole;
import com.athuahua.spzx.service.SysUserRoleService;
import com.athuahua.spzx.mapper.SysUserRoleMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author huahua
* @description 针对表【sys_user_role(用户角色)】的数据库操作Service实现
* @createDate 2024-07-22 21:58:20
*/
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>
    implements SysUserRoleService{

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public void doAssign(AssginRoleDto assginRoleDto) {
        // 查询当前角色的职位
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, assginRoleDto.getUserId());
        sysUserRoleMapper.delete(queryWrapper);

        //新增关系表
        assginRoleDto.getRoleIdList().forEach(roleId -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assginRoleDto.getUserId());
            sysUserRole.setRoleId(roleId);

            sysUserRole.setCreateTime(new DateTime());
            sysUserRole.setUpdateTime(new DateTime());
            sysUserRoleMapper.insert(sysUserRole);

        });
    }
}




